<script setup>
import router from "../../../../router";
import MyNav from "../../../../components/MyNav.vue";
import MyForm from "../../../../components/MyForm.vue";
import {RULE} from "../../../../const";
import {reactive, ref} from "vue";
import {insertApi} from "../../../../util/axios.js";

// 当前父菜单ID和菜单名称
let pid = sessionStorage.getItem('pid');
let pTitle = sessionStorage.getItem('pTitle');

/* ==================== 页面常量 ==================== */

// 路径导航
const navItems = [
  {icon: 'User', label: '用户管理'},
  {icon: 'Flag', label: '菜单列表（一级菜单）', url: '/Menu'},
  {icon: 'Flag', label: '菜单列表（二级菜单）', url: '/SubMenu'},
  {icon: 'Plus', label: '添加菜单（二级菜单）'},
];

/* ==================== 添加表单 ==================== */

// 表单项 + 表单值 + 表单规则
let items = ref([
  {label: '父级', prop: 'pTitle', disabled: true, span: 12},
  {label: '标题', prop: 'title', required: true, span: 12},
  {label: '地址', prop: 'url', required: true, span: 12},
  {label: '排序', prop: 'idx', type: 'number', min: 1, required: true, span: 12},
  {label: '图标', prop: 'icon', type: 'icon', required: true},
  {label: '描述', prop: 'info', type: 'textarea', required: true},
]);
let data = reactive({pTitle, pid, info: '暂无描述'});
let rules = {
  title: RULE.TITLE,
  url: RULE.MENU_URL,
  icon: RULE.MENU_ICON,
  info: RULE.INFO
};

/**
 * 添加成功后调用
 *
 * 1. 路由到 SubMenu 页面
 */
function insertSuccess() {
  router.push('/SubMenu');
}
</script>

<template>
  <my-nav :items="navItems"/>
  <el-divider/>
  <el-card className="sub-menu-insert-card" header="添加二级菜单">
    <my-form type="insert"
             :items="items"
             :rules="rules"
             :args="{module: 'menu'}"
             :api="insertApi"
             :params="data"
             :callback="insertSuccess"/>
  </el-card>
</template>

<style scoped lang="scss">
.sub-menu-insert-card {
  width: 70%; // 宽度
  margin: 20px auto 0; // 外边距
}
</style>